package com.acme.bankapp.domain.bank;

import java.util.Date;
import com.acme.bankapp.domain.account.Account;
import com.acme.bankapp.domain.client.Client;
import com.acme.bankapp.domain.client.ClientRegistrationListener;
import com.acme.bankapp.domain.exception.ClientExistsException;

public class Bank {
	
	private Client[] clients;
	private int countClients;
	private ClientRegistrationListener[] listeners;
	
	public Bank(){
		this.clients = new Client[10];
		countClients = 0;
		
               
	}
	
	public Client addClient(Client client) throws ClientExistsException{
		
		if(containsByName(client))
			throw new ClientExistsException("Client with name - " + client.getName() + " alredy exsist!");
		
		clients[countClients++] = client;
		
		
		return client;
	}
	

	private boolean containsByName(Client client){
		for (Client curclient : clients) {
			if(curclient != null && curclient.getName() == client.getName())
				return true;
		}	
		return false;
	}
	
	
	public Client getClient(Client client){
		int index = indexOf(client);
		return index >=0 ? clients[index] : null;
	}
	
	public Account getClientAccount(Client client){
		Client tmpclient = getClient(client);
		return tmpclient != null ? tmpclient.getAccount() : null; 
	}
	
	private int indexOf(Client client){
		int i=0;
		boolean find = false;
		for (i = 0; i < clients.length; i++) {
			Client array_element = clients[i];
			if(array_element!=null && array_element == client){
				find = true;
			}
		}
		return find ? i : -1;
	}
	
	public Client[] getClients() {
		Client[] tmp = new Client[countClients];
		int iter=0;
		for (Client client : clients) {
			if(client == null)
				continue;
			tmp[iter++] = client;
		}
		//Array.copy()
		return tmp;
	}

	public int getCountClients() {
		return countClients;
	}
		

}
